from collections import defaultdict

s = input()
n = len(s)
stack = []
pairs = []
prefix_freq = [defaultdict(int)]
cur = defaultdict(int)
for i, ch in enumerate(s):
    if ch == '(':
        stack.append(i)
    elif ch == ')':
        start = stack.pop()
        pairs.append((start, i))
    else:
        cur[ch] += 1
    prefix_freq.append(cur.copy())
del cur, stack, start, ch
keys = prefix_freq[-1].keys()
max_values = prefix_freq[-1].values()
# 普通数组用于构建前缀表
nums = dict()
for key, value in prefix_freq[-1].items():
    nums[key] = [0 for _ in range(value + 1)]
for left, right in pairs:
    for word in keys:
        display_cnt = prefix_freq[right + 1][word] - prefix_freq[left][word]
        if not display_cnt: continue
        nums[word][display_cnt] += 1

prefix = dict(defaultdict(list))
q = int(input())
for _ in range(q):
    c, x = input().split()
    x = int(x)

    print(prefix[c][-1] - prefix[c][int(x) - 1])
